<template>
  <div class="index">
    <div class="container" style="width: 1426px">
      <div class="swiper-box">
        <div class="nav-menu">
          <ul class="menu-wrap">
            <li class="menu-item">
              <a href="javascript:;">水果</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009902)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">水产</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009904)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">蔬菜</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009901)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">禽畜肉蛋</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009903)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">农资农机</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009905)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">种子种苗</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009906)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">农副加工</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009907)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
            <li class="menu-item">
              <a href="javascript:;">粮油米面</a>
              <div class="children">
                <ul>
                  <li v-for="(item, index) in menuList(1009908)" :key="index">
                    <a :href="item ? '/detail/' + item.id : 'javascript:;'">
                      <img :src="item ? item.mainImage : '/imgs/item-box-1.png'" alt="">
                      {{ item.name || 'null' }}
                    </a>
                  </li>
                </ul>
              </div>
            </li>
          </ul>
        </div>
        <swiper :options="swiperOption">
          <swiper-slide v-for="(item, index) in slideList" :key="index">
            <a :href="item.status === 1 ? '/detail/' + item.id : '/bannerPage/' + item.id"><img :src="item.img" alt=""></a>
          </swiper-slide>
          <div class="swiper-pagination" slot="pagination"></div>
          <div class="swiper-button-prev" slot="button-prev"></div>
          <div class="swiper-button-next" slot="button-next"></div>
        </swiper>
      </div>
      <div class="ads-box">
        <a :href="item.status === 1 ? '/detail/' + item.id : '/bannerPage/' + item.id" v-for="(item, index) in adsList" :key="index">
          <img v-lazy="item.img" alt="">
        </a>
      </div>
      <div class="banner">
        <a href="/bannerPage/1009902">
          <img
              v-lazy="'https://img.zcool.cn/community/0150e155f936346ac7251df8f3d48b.jpg@1280w_1l_2o_100sh.jpg'"
              alt="">
        </a>
      </div>

    </div>
    <div class="product-box">
      <div class="container">
        <h2>水果</h2>
        <div class="wrapper">
          <div class="banner-left">
            <a href="/bannerPage/1009902">
              <img
                  v-lazy="'https://s1.ax1x.com/2023/05/27/p9LwNbd.jpg'"
                  alt="">
            </a>
          </div>
          <div class="list-box">
            <div class="list" v-for="(arr, i) in productFilterList(1009902)" :key="i">
              <div class="item" v-for="(item, j) in arr" :key="j">
                                        <span :class="j%2 == 0 ? 'new-pro' : 'kill-pro'">
                                            {{ j % 2 == 0 ? '新品' : '好货推荐' }}
                                        </span>
                <div class="item-img" @click="goToDetail(item.id)">
                  <img v-lazy="item.mainImage" alt="">
                </div>
                <div class="item-info">
                  <h3>{{ item.name }}</h3>
                  <p>{{ item.subtitle }}</p>
                  <p class="price" @click="addCart(item.id)">{{ item.price }} <span>元</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="product-box">
      <div class="container">
        <h2>水产</h2>
        <div class="wrapper">
          <div class="banner-left">
            <a href="/bannerPage/1009904">
              <img
                  v-lazy="'https://s1.ax1x.com/2023/05/27/p9LwaVA.jpg'"
                  alt="">
            </a>
          </div>
          <div class="list-box">
            <div class="list" v-for="(arr, i) in productFilterList(1009904)" :key="i">
              <div class="item" v-for="(item, j) in arr" :key="j">
                                        <span :class="j%2 == 0 ? 'new-pro' : 'kill-pro'">
                                            {{ j % 2 == 0 ? '新品' : '好货推荐' }}
                                        </span>
                <div class="item-img" @click="goToDetail(item.id)">
                  <img v-lazy="item.mainImage" alt="">1
                </div>
                <div class="item-info">
                  <h3>{{ item.name }}</h3>
                  <p>{{ item.subtitle }}</p>
                  <p class="price" @click="addCart(item.id)">{{ item.price }} <span>元</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="product-box">
      <div class="container">
        <h2>蔬菜</h2>
        <div class="wrapper">
          <div class="banner-left">
            <a href="/bannerPage/1009901">
              <img
                  v-lazy="'https://s1.ax1x.com/2023/05/27/p9LwGvD.jpg'"
                  alt="">
            </a>
          </div>
          <div class="list-box">
            <div class="list" v-for="(arr, i) in productFilterList(1009901)" :key="i">
              <div class="item" v-for="(item, j) in arr" :key="j">
                                        <span :class="j%2 == 0 ? 'new-pro' : 'kill-pro'">
                                            {{ j % 2 == 0 ? '新品' : '好货推荐' }}
                                        </span>
                <div class="item-img" @click="goToDetail(item.id)">
                  <img v-lazy="item.mainImage" alt="">
                </div>
                <div class="item-info">
                  <h3>{{ item.name }}</h3>
                  <p>{{ item.subtitle }}</p>
                  <p class="price" @click="addCart(item.id)">{{ item.price }} <span>元</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="product-box">
      <div class="container">
        <h2>粮油米面</h2>
        <div class="wrapper">
          <div class="banner-left">
            <a href="/bannerPage/1009908">
              <img
                  v-lazy="'https://s1.ax1x.com/2023/05/27/p9LwdUI.jpg'"
                  alt="">
            </a>
          </div>
          <div class="list-box">
            <div class="list" v-for="(arr, i) in productFilterList(1009908)" :key="i">
              <div class="item" v-for="(item, j) in arr" :key="j">
                                        <span :class="j%2 == 0 ? 'new-pro' : 'kill-pro'">
                                            {{ j % 2 == 0 ? '新品' : '好货推荐' }}
                                        </span>
                <div class="item-img" @click="goToDetail(item.id)">
                  <img v-lazy="item.mainImage" alt="">
                </div>
                <div class="item-info">
                  <h3>{{ item.name }}</h3>
                  <p>{{ item.subtitle }}</p>
                  <p class="price" @click="addCart(item.id)">{{ item.price }} <span>元</span></p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <service-bar></service-bar>
    <modal
        title="提示"
        sureType="查看购物车"
        btnType="1"
        modalType="middle"
        :showModal="showModal"
        @submit="goToCart"
        @cancel="showModal = false">
      <template slot="body">
        <p>添加商品成功！</p>
      </template>
    </modal>
  </div>
</template>

<script>
import Modal from '@/components/Modal.vue';
import ServiceBar from '@/components/ServiceBar.vue';
import 'swiper/dist/css/swiper.css';
import {swiper, swiperSlide} from 'vue-awesome-swiper';

export default {
  name: 'nong-index',
  components: {
    ServiceBar,
    swiper,
    swiperSlide,
    Modal
  },
  data() {
    return {
      swiperOption: {
        autoplay: true,
        loop: true,
        effect: 'cube',
        cubeEffect: {
          slideShadows: false,
          shadow: false,
          shadowOffset: 100,
          shadowScale: 0.6
        },
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      },
      slideList: [
        {
          id: '18490',
          img: 'https://s1.ax1x.com/2023/06/12/pCeEkIH.jpg',
          status: 1
        },
        {
          id: '18480',
          img: 'https://s1.ax1x.com/2023/06/12/pCeEsY9.jpg',
          status: 1
        },
        {
          id: '18476',
          img: 'https://s1.ax1x.com/2023/06/12/pCeE8ij.jpg',
          status: 1
        },
        {
          id: '1009901',
          img: 'https://s1.ax1x.com/2023/06/12/pCeAxR1.jpg',
          status: 2
        },
        // {
        //   id: '17971',
        //   img: 'https://s1.ax1x.com/2023/05/26/p9qMSS0.jpg'
        // }
      ],
//       menuList: [
//         [
//           {
//             id: 18443,
//             img: '/imgs/item-box-1.png',
//             name: '小米CC9'
//           },
// /*          {
//             id: 18443,
//             img: '/imgs/item-box-2.png',
//             name: '小米8青春版'
//           },
//           {
//             id: 18443,
//             img: '/imgs/item-box-3.jpg',
//             name: 'Redmi K20 Pro'
//           },*/
//           {
//             id: 18443,
//             img: '/imgs/item-box-4.jpg',
//             name: '移动4G专区'
//           },
//         ],
//         [0, 0, ], [0, 0,], [ 0, 0], [0, 0,], [0, 0,],
//       ],
      adsList: [
        {
          id: 1009902,
          img: 'https://s1.ax1x.com/2023/06/12/pCeEoYd.jpg',
          status: 2
        },
        {
          id: 18537,
          img: 'https://s1.ax1x.com/2023/06/12/pCeETfA.jpg',
          status: 1
        },
        {
          id: 1009904,
          img: 'https://s1.ax1x.com/2023/06/12/pCeEj0S.jpg',
          status: 2
        },
        {
          id: 1009908,
          img: 'https://s1.ax1x.com/2023/06/12/pCeVCpn.jpg',
          status: 2
        },
      ],
      phoneList: [],
      productList: [],
      showModal: false
    }
  },
  mounted() {
    this.init()
    this.getProductList()
  },
  computed: {},
  methods: {
    init() {
      this.request.get('/products', {
        params: {
          categoryId: 1009902,
          pageSize: 999
        }
      }).then((res) => {
        res.list = res.list.slice(4, 12)
        this.phoneList = [res.list.slice(0, 4), res.list.slice(4, 8)]
      })
    },
    getProductList() {
      this.request.get('/products/list').then((res) => {
        this.productList = res
      })
    },
    menuList(categoryId) {
      const menu = this.productList.filter(item =>
          item.categoryId === categoryId
      )
      // console.log(menu)
      return menu
    },
    //过滤商品分类
    productFilterList(categoryId) {
      let product = this.productList.filter(item =>
          item.categoryId === categoryId
      )
      return [product.slice(0, 4), product.slice(4, 8)]
    },

    addCart(id) {
      this.request.post('/carts', {
        productId: id,
        selected: true
      }).then((res) => {
        this.showModal = true
        // console.log(res.cartTotalQuantity);
        this.$store.dispatch('saveCartCount', res.cartTotalQuantity)
      })
    },
    goToCart() {
      this.$router.push('/cart')
    },
    goToDetail(id) {
      const routeUrl = this.$router.resolve({
        name: "detail",
        params: {
          id
        }
      });
      window.open(routeUrl.href, "_blank");
    },
  }
}
</script>

<style lang="scss" scoped>
@import '../assets/scss/base.scss';
@import '../assets/scss/config.scss';
@import '../assets/scss/mixin.scss';

.index {
  .swiper-box {
    .nav-menu {
      position: absolute;
      width: 264px;
      height: 451px;
      z-index: 9;
      padding: 26px 0;
      background-color: #55585A7a;
      box-sizing: border-box;

      .menu-wrap {
        .menu-item {
          height: 50px;
          line-height: 50px;

          a {
            position: relative;
            display: block;
            font-size: 16px;
            color: $colorG;
            padding-left: 30px;

            &:after {
              position: absolute;
              right: 30px;
              top: 17.5px;
              content: '';
              @include bgImg(10px, 15px, '/public/imgs/icon-arrow.png')
            }
          }

          &:hover {
            background-color: $colorA;

            .children {
              display: block;
            }
          }

          .children {
            display: none;
            width: 481px;
            height: 451px;
            background-color: $colorG;
            position: absolute;
            top: 0;
            left: 264px;
            border: 1px solid $colorH;

            ul {
              //display: flex;
              //justify-content: space-between;
              height: 75px;

              li {
                box-sizing: border-box;
                width: 240px;
                height: 75px;
                line-height: 75px;
                //flex: 1;
                display: inline-block;
                padding-left: 23px;

                a {
                  color: $colorB;
                  font-size: 14px;

                  &:after {
                    display: none;
                  }
                  &:hover {
                    color: $colorA;
                    font-weight: bold;
                  }
                }

                img {
                  width: 42px;
                  height: 35px;
                  vertical-align: middle;
                  margin-right: 15px;
                }
              }
            }
          }
        }
      }
    }

    .swiper-container {
      height: 451px;

      .swiper-button-prev {
        left: 274px;
      }

      img {
        width: 100%;
        // object-fit: cover;
        height: 100%;
      }
    }
  }

  .ads-box {
    // min-height:167px;
    @include flex();
    margin: 14px 0 31px;

    a {
      //width: 296px;
      width: 336px;
      height: 167px;
      transition: all .2s;

      &:hover {
        box-shadow: 0 10px 20px rgba(0, 0, 0, .2);
        transform: translate3d(0, -2px, 0);
      }
    }
  }

  .banner {
    height: 280px;
    margin-bottom: 50px;
    img {
      object-fit: cover;
    }
  }

  .product-box {
    background-color: $colorJ;
    padding: 30px 0 50px;

    h2 {
      font-size: $fontF;
      height: 21px;
      line-height: 21px;
      color: $colorB;
      margin-bottom: 20px;
    }

    .wrapper {
      display: flex;

      .banner-left {
        margin-right: 16px;

        img {
          width: 224px;
          height: 619px;
          //object-fit: cover;
        }
      }

      .list-box {
        .list {
          @include flex();
          width:986px;
          margin-bottom: 14px;

          &:last-child {
            margin-bottom: 0;
          }

          .item {
            width: 236px;
            height: 302px;
            background-color: $colorG;
            text-align: center;
            transition: all .2s;

            &:hover {
              box-shadow: 0 15px 30px rgba(0, 0, 0, .1);
              transform: translate3d(0, -2px, 0);
            }

            span {
              display: inline-block;
              width: 67px;
              height: 24px;
              font-size: 14px;
              line-height: 24px;
              color: $colorG;

              &.new-pro {
                background-color: rgba(126, 207, 104, 0.73);
              }

              &.kill-pro {
                background-color: rgba(232, 38, 38, 0.62);
              }
            }

            .item-img {
              text-align: center;

              img {
                height: 195px;
              }
            }

            .item-info {
              h3 {
                font-size: $fontJ;
                color: $colorB;
                line-height: $fontJ;
                font-weight: bold;
              }

              p {
                max-width: 210px;
                color: $colorD;
                line-height: 13px;
                margin: 6px auto 13px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
              }
            }

            .price {
              color: #F20A0A;
              font-size: $fontJ;
              font-weight: bold;
              cursor: pointer;
              span {
                font-size: 8px;
                color: #888888;
                display: inline;
              }

              &:after {
                content: '';
                @include bgImg(22px, 22px, '../../public/imgs/icon-cart-hover.png');
                margin-left: 5px;
                vertical-align: middle;
              }
            }
          }
        }
      }
    }
  }
}
</style>